package com.zang.blogz.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zang.blogz.annotation.OptLog;
import com.zang.blogz.constant.OptTypeConstant;
import com.zang.blogz.model.input.ro.CommentRO;
import com.zang.blogz.model.output.CommentVO;
import com.zang.blogz.model.output.admin.AdminCommentVO;
import com.zang.blogz.result.Result;
import com.zang.blogz.service.CommentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 朵橙i
 * @since 2022-06-17
 */
@RestController
@RequestMapping("/blogz/comment")
@Api(tags = "文章评论")
public class CommentController {

    @Autowired
    private CommentService commentService;

    @ApiOperation(value = "根据文章id查询评论列表", notes = "根据文章id查询评论列表")
    @GetMapping("/")
    public List<CommentVO> getCommentByArticleId(Integer articleId) {

        return commentService.getCommentByArticleId(articleId);
    }

    @ApiOperation(value = "查询未通过审批的评论", notes = "查询未通过审批的评论")
    @GetMapping("/admin/didNotPass")
    public Result<IPage<AdminCommentVO>> didNotPass(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                    @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize){

        Page<AdminCommentVO> page = new Page<>(pageNo, pageSize);

        return Result.ok(commentService.didNotPass(page));
    }

    @ApiOperation(value = "查询已通过审批的评论", notes = "查询已通过审批的评论")
    @GetMapping("/admin/pass")
    public Result<IPage<AdminCommentVO>> pass(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                              @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize){

        Page<AdminCommentVO> page = new Page<>(pageNo, pageSize);

        return Result.ok(commentService.pass(page));
    }

    @ApiOperation(value = "添加评论")
    @PostMapping("/login")
    @OptLog(optType = OptTypeConstant.SAVE)
    public Result<CommentRO> addComment(@RequestBody CommentRO commentRo) {

        return commentService.addComment(commentRo);
    }



    @ApiOperation(value = "开启评论审批")
    @PostMapping("/admin/approvalFlag/turnOn")
    @OptLog(optType = OptTypeConstant.UPDATE)
    public Result<String> turnOnApprovalFlag(){

        return commentService.updateApprovalFlag();
    }

    @ApiOperation(value = "关闭评论审批")
    @PostMapping("/admin/approvalFlag/closure")
    @OptLog(optType = OptTypeConstant.UPDATE)
    public Result<String> closureApprovalFlag(){

        return commentService.closureApprovalFlag();
    }

    @ApiOperation(value = "开启邮件通知功能")
    @PostMapping("/admin/turnOnEmailNotifications")
    @OptLog(optType = OptTypeConstant.UPDATE)
    public Result<String> turnOnEmailNotifications(){

        return commentService.turnOnEmailNotifications();
    }

    @ApiOperation(value = "关闭邮件通知功能")
    @PostMapping("/admin/closureOnEmailNotifications")
    @OptLog(optType = OptTypeConstant.UPDATE)
    public Result<String> closureOnEmailNotifications(){

        return commentService.closureOnEmailNotifications();
    }

    @ApiOperation(value = "通过审核")
    @PostMapping("/admin/approved")
    @OptLog(optType = OptTypeConstant.UPDATE)
    public Result<String> approved(@RequestParam(value = "commentId") String commentId){

        return commentService.approved(commentId);
    }



}
